[[ package gen;

import java.net.*;
import java.util.*;

import se.rupy.http.*;
import se.rupy.memory.*;
import se.rupy.sprout.*;
import se.rupy.content.*;

public class content extends Sprout {
	public String path() { return "/content"; }
	public void filter(Event event) throws Event, Exception {
		Output out = event.output();
		event.query().parse();
		
		Iterator articles = null;
		
		int total = Article.PAGE;
		int page = event.query().medium("page", 0);

		Object key = event.session().get("key");
		String query = event.string("query");
		boolean most = event.bit("most");

		if(most) {
            articles = Article.most(ARTICLE_READ, page, total).iterator();
		}
		else if(query.length() > 0) {
			articles = Article.query(query, page, total).iterator();
		}
		else {
			articles = Article.get("article", page, total).iterator();
		}
		
		while(articles.hasNext()) {
			Article article = (Article) articles.next();
			Node node = new Node(USER);
			
			try {
				node = (Node) article.child(USER).getFirst();
			}
			catch(Exception e) {
				//
			}
			
			int permit = article.permit(key); ]]

    <div class="article">
      <table width="100">
        <tr>
          <td colspan="2"><a href="article?id=[[ article.getId() ]]"><h2><center>[[ article.meta(ARTICLE_TITLE) ]]</center></h2></a></td>
        </tr>
        <tr>
          <td><i>[[ article.time() ]]</i></td>
          <td align="right">[[ if(article.comments > 0) { ]]<i style="vertical-align: top;">[[ article.comments ]]</i>&nbsp;[[ } ]]<a href="article?id=[[ article.getId() ]]"><img src="res/comment.gif" alt="[[ i18n("Comment") ]]"/></a></td>
        </tr>
        <tr>
          <td colspan="2">

[[ print(article, event); ]]

          </td>
        </tr>
        <tr>
          <td colspan="2">/<a href="/[[ node.safe(USER_NAME) ]]">[[ node.safe(USER_NAME) ]]</a></td>
        </tr>
        
[[			if(permit > 0) { ]]
        
        <tr>
          <td colspan="2">

            <a href="/edit?id=[[ article.getId() ]]" class="button">
              <span>[[ i18n("Edit") ]]</span>
            </a>

            <a href="/delete?id=[[ article.getId() ]]" class="button" onclick="return confirm('[[ i18n("Are you sure you want to delete the article?") ]]');">
              <span>[[ i18n("Delete") ]]</span>
            </a>
            
          </td>
        </tr>
        
[[			} ]]
        
      </table>
    </div>

[[ 		} ]]

[[	}
	static void print(Article article, Event event) throws Exception {
		Output out = event.output();
		Iterator it = article.child(FILE).iterator();
		boolean image = false;
		boolean audio = false;
		
		while(it.hasNext()) {
			Node file = (Node) it.next();
			Data name = file.meta(FILE_NAME);
			Data type = file.meta(FILE_TYPE);
			
			if(type != null) {
				if(type.equals("IMAGE") && !image) { ]]

          <!--div><a class="title" href="article?id=[[ article.getId() ]]"><img class="rounded" src="[[ File.path(event, file, name, "-200.jpeg") ]]" /></a></div-->
          <div><img class="rounded" src="[[ File.path(event, file, name, "-200.jpeg") ]]" /></div>

[[					image = true;
				} else if(type.equals("AUDIO") && !audio) {
					String path = "file" + file.encoded() + URLEncoder.encode("/" + name.getString(), "UTF-8") + ".mp3"; ]]

          <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash" height="30" width="200">
            <param name="movie" value="audio.swf?audio_file=[[ path ]]&color=E4E4E4"/>
            <param name="wmode" value="normal"/><!-- should be opaque so flash doesen't go ontop of everything, 
            										 but webkit doesen't let click events down to the 
            										 flash object in that case for some reason :( -->
            <embed src="audio.swf?audio_file=[[ path ]]&color=E4E4E4" wmode="normal" height="30" width="200" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/>
          </object>

[[					audio = true;
				}
			}
		}

		LinkedList columns = article.columnize();
		it = columns.iterator();
		if(it.hasNext()) {
			String column = (String) it.next(); ]]

          <div class="column">
            [[ column ]]
            [[ if(columns.size() > 1) { ]]
            [[ out.print("&#8230;"); ]]
			[[ } ]]
          </div>

[[		}
	}
} ]]

